package com.hksj.em.service;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hksj.em.entity.Surgical;
import com.hksj.em.mapper.SurgicalMapper;
import com.hksj.em.query.CallCaregiverQuery;
import com.hksj.em.query.SurgicalQuery;
import com.hksj.em.vo.SurgicalFromHisVO;
import com.hksj.em.vo.SurgicalVO;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @author zengke
 * @version 1.0
 * @see SurgicalService
 * @since 2025/03/25 10:28
 */

@Service
public class SurgicalService extends ServiceImpl<SurgicalMapper, Surgical> {

    /**
     * 分页查询排台列表
     * @param query 排台查询对象
     * @return 分页结果
     */
    public IPage<SurgicalVO> querySurgicalListByPaging(SurgicalQuery query) {

        //创建分页对象
        IPage<SurgicalVO> page = new Page<>(query.getPageNum(),query.getPageSize());

        //分页查询排台列表
        List<SurgicalVO> surgicalList = baseMapper.querySurgicalListByPaging(page, query);

        //将分页结果设置到分页对象中
        page.setRecords(surgicalList);

        //返回分页对象
        return page;
    }

    /**
     * 动态查询排台列表
     * @param query 排台查询对象
     * @return 排台列表
     */
    public List<SurgicalVO> querySurgicalList(SurgicalQuery query) {

        //动态查询所有的排台列表
        List<SurgicalVO> surgicalList = baseMapper.querySurgicalListByPaging(null, query);

        //返回查询结果
        return surgicalList;
    }

    /**
     * 同步排台信息
     * @return 是否同步成功
     */
    public String syncSurgical() {

        //查询his源的手术室信息
        List<SurgicalFromHisVO> surgicalFromHisList = baseMapper.querySurgicalFromHisList();

        //查询当前库已有的手术室
        List<Surgical> currentSurgicalList = super.list();

        //插入数据集、his中有而current中没有
        List<Surgical> insertTempList = new ArrayList<>();

        //更新数据集、his中有current中也有
        List<Surgical> updateTemplList = new ArrayList<>();

        surgicalFromHisList.forEach(his -> {

            //当前库为空、直接同步his库的数据
            if (currentSurgicalList == null){
                Surgical surgical = Convert.convert(Surgical.class, his);
                surgical.setId(null);

                insertTempList.add(surgical);
            } else{
                //若当前库不为空且his库中的数据不在当前库中
                if (!currentSurgicalList.stream().map(current -> current.getPatientId()+"_"+current.getSurgicalId()).collect(Collectors.toList()).contains(his.getPatientId()+"_"+his.getSurgicalId())){
                    Surgical surgical = Convert.convert(Surgical.class, his);
                    surgical.setId(null);

                    insertTempList.add(surgical);
                } else {

                    //his库的数据在当前库中 此时做更新
                    Surgical surgical = Convert.convert(Surgical.class, his);
                    surgical.setId(UUID.randomUUID().toString().replaceAll("_",""));
                    updateTemplList.add(surgical);
                }
            }
        });

        //做新增的业务
        if (!ObjectUtils.isEmpty(insertTempList)){
            super.saveBatch(insertTempList);
        }

        //更新的业务
        if (!ObjectUtils.isEmpty(updateTemplList)){
            batchUpdateByPatientIdAndSurgicalId(updateTemplList);
        }

        return "同步排台数据成功";
    }

    public boolean batchUpdateByPatientIdAndSurgicalId(List<Surgical> surgicalList) {
        return executeBatch(surgicalList, (sqlSession,surgical) -> {
            // 使用update方法，传入实体对象和更新条件
            update(surgical,
                    Wrappers.<Surgical>lambdaUpdate()
                            .eq(Surgical::getPatientId, surgical.getPatientId())
                            .eq(Surgical::getSurgicalId, surgical.getSurgicalId())
            );
        });
    }

    public IPage<SurgicalVO> querySurgeryStatusForFamilyWaitingArea(CallCaregiverQuery callCaregiverQuery) {

        //创建分页对象
        IPage<SurgicalVO> page = new Page<>(callCaregiverQuery.getPageNum(),callCaregiverQuery.getPageSize());

        //分页查询排台列表
        List<SurgicalVO> surgicalList = baseMapper.querySurgeryStatusForFamilyWaitingArea(page);

        page.setRecords(surgicalList);
        return page;

    }
}
